package com.example.blog_system_ssm.service;

import com.example.blog_system_ssm.common.SnowFlakeUtils;
import com.example.blog_system_ssm.mapper.ArticleMapper;
import com.example.blog_system_ssm.mapper.DraftMapper;
import com.example.blog_system_ssm.model.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DraftService {
    @Autowired
    private DraftMapper draftMapper;
    @Autowired
    private ArticleMapper articleMapper;

    //初始化一个雪花算法工具类，设置机房ID和机器ID都为0
    SnowFlakeUtils snowFlakeUtils=new SnowFlakeUtils(0,0);

    public int add(Long uid,String title,String content){
        return draftMapper.add(uid, snowFlakeUtils.nextId(), title, content);
    }
    public List<ArticleInfo> getMyDraftList(Long uid){
        return draftMapper.getMyDraftList(uid);
    }
    public int delete(Long aid,Long uid){return draftMapper.delete(aid,uid);}
    public ArticleInfo getDetail(Long aid,Long uid){return draftMapper.getDetail(aid,uid);}

    public int update(Long aid, Long uid, String title, String content) {
        return draftMapper.update(aid, uid, title, content);
    }

    @Transactional
    public int submit(Long uid,Long aid,String title,String content){
        draftMapper.delete(aid,uid);
        //更新文章总数
        articleMapper.incrArticleCount(uid,1);
        return draftMapper.submit(uid, snowFlakeUtils.nextId(), title,content);
    }
}
